<template>
	<view>
		<u-gap height="16" bg-color="#F8F8F8"></u-gap>
		<view>
			<u-form :model="form" ref="uForm" class="uForm" :border-bottom="false" label-position="top">
				<u-form-item :border-bottom="false" label="设备">
					<view class="num-text">
						<u-field
							v-model="form.equipmentName"
							:disabled="true"
							:border-bottom="false"
							label-width="0"
							placeholder="请选择设备"
						>
							<image slot="right" src="/static/icon-jiantou.png" mode="" @click="showAction"></image>
						</u-field>
					</view>
				</u-form-item>
				<u-gap height="16" bg-color="#F8F8F8"></u-gap>
				<u-form-item :border-bottom="false" label="截止时间">
					<view class="num-text">
						<u-field
							v-model="form.endDate"
							:disabled="true"
							:border-bottom="false"
							label-width="0"
							placeholder="请选择截止时间"
						>
							<image slot="right" src="/static/icon-jiantou.png" mode="" @click="showActionTime"></image>
						</u-field>
						<u-picker mode="time" v-model="endDateShow" @confirm="confirm"></u-picker>
					</view>
				</u-form-item>
				<u-gap height="16" bg-color="#F8F8F8"></u-gap>
				<u-form-item :border-bottom="false" label="备注">
					<view class="num-text">
						<u-input v-model="form.remark" type="textarea" maxlength="100" placeholder="备注" />
						<view class="font-num">0/100</view>
					</view>
				</u-form-item>
			</u-form>
			<u-row gutter="32" class="bottom-box" justify="center">
				<u-col span="11">
					<view><u-button type="primary" @click="submitHandle">提交</u-button></view>
				</u-col>
			</u-row>
		</view>
		
		<u-popup v-if="popupShow" v-model="popupShow" mode="bottom" :safe-area-inset-bottom="true" border-radius="8">
			<view class="dialog">
				<view class="dialog-header">
					<text :class="titleVal >= 1 ?'text-color' : ''" @click="clickTitleHandle(1)">请选择项目</text>
					<image src="/static/icon-jiantou.png" mode=""></image>
					<text :class="titleVal >= 2 ?'text-color' : ''" @click="clickTitleHandle(2)">请选择片区</text>
					<image src="/static/icon-jiantou.png" mode=""></image>
					<text :class="titleVal >= 3 ?'text-color' : ''" @click="clickTitleHandle(3)">请选择设备</text>
				</view>
				<u-gap height="16" bg-color="#f5f5f5"></u-gap>
				<view class="popup-list" v-if="titleVal === 1">
					<view class="popup-li" v-for="(item, index) in projectData" :key="index">
						<text>{{ item.projectName }}</text>
						<u-checkbox v-model="item.checked" shape="square" @change="changeProject(item)"></u-checkbox>
					</view>
				</view>
				<view class="popup-list" v-if="titleVal === 2">
					<view class="popup-li" v-for="(item, index) in areaData" :key="index">
						<text>{{ item.areaName }}</text>
						<u-checkbox v-model="item.checked" shape="square" @change="changeArea(item)"></u-checkbox>
					</view>
				</view>
				<view class="popup-list" v-if="titleVal === 3">
					<view class="popup-li" v-for="(item, index) in equipmentData" :key="index">
						<text>{{ item.equipmentName }}</text>
						<u-checkbox v-model="item.checked" shape="square" @change="changeEquipment(item)"></u-checkbox>
					</view>
				</view>
				<view class="popup-footer">
					<u-row gutter="0" justify="between">
						<u-col span="5.8">
							<u-button @click="cancel">取消</u-button>
						</u-col>
						<u-col span="5.8">
							<u-button type="primary" @click="deviceSubmit">确定</u-button>
						</u-col>
					</u-row>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				popupShow: false,
				titleVal: 1,
				projectData: [],
				areaData: [],
				equipmentData: [],
				endDateShow: false,
				form: {
					equipmentName: '',
					endDate: '',
					remark: ''
				}
			};
		},
		mounted() {
			this.projectHandlerList()
		},
		methods: {
			showAction() {
				this.popupShow = true
			},
			projectHandlerList() {
				this.$u.api.projectList().then(res => {
					if (res.code === 200 && res.rows.length) {
						this.projectData = res.rows.map(item => {
							return {...item, checked: false}
						})
					}
				})
			},
			/** 选择项目 */
			changeProject(val) {
				this.projectData = this.projectData.map(item => {
					if (val.id === item.id) {
						item.checked = true
					} else {
						item.checked = false
					}
					return {...item}
				})
				this.titleVal++
				this.equipmentData = []
				this.areaData = []
				this.$u.api.areaList().then(res => {
					if (res.code === 200 && res.rows.length) {
						res.rows.forEach(item => {
							if (val.id === item.projectId) {
								this.areaData.push({...item, checked: false})
							}
						})
					}
				})
			},
			/** 选择片区 */
			changeArea(val) {
				this.areaData = this.areaData.map(item => {
					if (val.id === item.id) {
						item.checked = true
					} else {
						item.checked = false
					}
					return {...item}
				})
				this.titleVal++
				this.equipmentData = []
				this.$u.api.equipmentList().then(res => {
					if (res.code === 200 && res.rows.length) {
						res.rows.map(item => {
							if (val.id === item.areaId) {
								this.equipmentData.push({...item, checked: false})
							}
						})
					}
				})
			},
			/** 选择设备 */
			changeEquipment(val) {
				console.log(val)
				this.equipmentData = this.equipmentData.map(item => {
					if (val.id === item.id) {
						item.checked = true
					} else {
						item.checked = false
					}
					return {...item}
				})
				this.form = val
			},
			clickTitleHandle(val) {
				this.titleVal = val
			},
			cancel() {
				this.popupShow = false
				this.titleVal = 1
				this.form = {}
				this.equipmentData = []
				this.areaData = []
				this.projectHandlerList()
			},
			showActionTime() {
				this.endDateShow = true
			},
			confirm(e) {
				this.form.endDate = `${e.year}-${e.month}-${e.day}`
			},
			deviceSubmit() {
				this.popupShow = false
			},
			submitHandle() {
				this.$u.api.addPatrolList({ ...this.form, equipmentId: this.form.id, id: null }).then(res => {
					console.log(res)
					if (res.code == 200) {
						wx.navigateBack()
					}
				})
			},
			navTo(href) {
				uni.navigateTo({
					url: href
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
.uForm {
	.u-form-item {
		background: #fff;
		padding: 20rpx 30rpx;
	}
	::v-deep .u-form-item--left__content__label {
		font-family: PingFangSC, PingFang SC;
		font-weight: 400;
		font-size: 32rpx;
		color: #333333;
		line-height: 44rpx;
		text-align: left;
		font-style: normal;
	}
	.u-field {
		padding: 0;
		::v-deep .u-flex {
			margin: 0 !important;
		}
	}
}
.num-text {
	width: 100%;
	display: flex;
	flex-direction: column;
	::v-deep .u-button-wrap {
		display: flex;
	}
	image {
		width: 44rpx;
		height: 44rpx;
	}
}
.font-num {
	text-align: right;
	font-family: PingFangSC, PingFang SC;
	font-weight: 400;
	font-size: 28rpx;
	color: #999999;
	line-height: 1;
	text-align: right;
	font-style: normal;
}
::v-deep .u-form-item--left__content__label {
	line-height: 1;
}
.popup-footer {
	width: 100%;
	display: flex;
	padding: 30rpx;
	background: #fff;
	.u-row {
		width: 100%;
	}
}
.upload-box {
	::v-deep .u-list-item {
		margin-left: 0;
	}
}
.dialog {
	.dialog-header {
		display: flex;
		align-items: center;
		padding: 24rpx;
		.text-color {
			color: #3C7EFF;
		}
		text {
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #999999;
			line-height: 40rpx;
			text-align: left;
			font-style: normal;
		}
		image {
			width: 44rpx;
			height: 44rpx;
		}
	}
	.popup-list {
		.popup-li	 {
			padding: 28rpx 30rpx;
			display: flex;
			justify-content: space-between;
			text {
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #333333;
				line-height: 40rpx;
				text-align: left;
				font-style: normal;
			}
		}
	}
	.popup-footer {
		width: 100%;
		display: flex;
		padding: 0 30rpx 40rpx 30rpx;
		.u-row {
			width: 100%;
		}
	}
}
</style>
